<?php
/**
* alltosun.com push_group_user.php
================================================
* 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* @author: 祝利柯 (zhulk@alltosun.com)
* @date:2015-2-26
* $$Id: push_group_user.php 222074 2015-04-08 08:13:32Z zhulk $$
*/

class Action
{
    private $per_page = 20;

    public function __call($push = '', $params = array())
    {
        $group_id           = tools_helper::get('id', 0);

        $page_no = tools_helper::get('page_no', 1);
        $search_filter      = tools_helper::get('search_filter', array());

        $order = ' ORDER BY `id` DESC ';
        $filter = array(1=>1);
        $filter['push_group_id'] = $group_id;

        if (!$group_id) {
            return '参数错误!';
        }

        // ID
        if (isset($search_filter['id']) && $search_filter['id']) {
            $filter['id'] = $search_filter['id'];
        }

        // 用户ID
        if (isset($search_filter['user_id']) && $search_filter['user_id']) {
            $filter['user_id'] = $search_filter['user_id'];
        }

        // 时间
        if (isset($search_filter['start_time']) && $search_filter['start_time']) {
            $filter['add_time >='] = $search_filter['start_time'] . ' 00:00:00';
        }
        if (isset($search_filter['end_time']) && $search_filter['end_time']) {
            $filter['add_time <='] = $search_filter['end_time'] . ' 23:59:59';
        }

        $list  = array();
        $total = _model('push_group_user')->getTotal($filter);
        if ($total) {
            // 取id
            $user_ids = _model('push_group_user')->getFields('user_id', $filter);
            $user_ids = array_unique($user_ids);
            $user_names = array();
            $id_name_list = _model('user')->field('id', 'nick_name')->getList($user_ids);
            foreach ($id_name_list as $id_name) {
                $user_names[$id_name['id']] = $id_name['nick_name'];
            }

            $user_list = array();
            $list = get_data_list('push_group_user', $filter, $order, $page_no, $this->per_page);
            foreach ($list as $info) {
                if (isset($user_names[$info['user_id']])) {
                    $info['user_name'] = $user_names[$info['user_id']];
                }
                $user_list[] = $info;
            }
            Response::assign('user_list', $user_list);
        }

        Response::assign('id', $group_id);
        Response::assign('search_filter', $search_filter);
        Response::display('admin/group_user/list.html');
    }

    /**
     * 添加成员
     */
    public function add() {
         $id = tools_helper::get('id', 0);

         if (!$id) {
            return '参数错误!';
         }

         Response::assign('id', $id);
         Response::display('admin/group_user/add.html');
    }

    /**
     * 保存组成员
     */
    public function save()
    {
        $info = tools_helper::post('info', array());

        if (!isset($info['push_group_id']) || !$info['push_group_id']) {
            return '缺少角色!';
        }

        if (!isset($info['user_id']) || !$info['user_id']) {
            return '缺少用户!';
        }

        $user_info = _uri('user', $info['user_id']);
        if (!$user_info) {
            return '当前用户不存在!';
        }

        // 目前只允许单一角色
        $push_group_user_info = _model('push_group_user')->getList(array('user_id'=>$info['user_id']));
        if ($push_group_user_info) {
            return '该用户已经在当前推送组或其他推送组了';
        }

        // 通知极光
        // $reg_ids = user_helper::get_user_reg_ids($info['user_id']);
        $reg_ids = _widget('push')->get_user_reg_ids($info['user_id']);
        $reg_ids = array_values($reg_ids);
        // 检测是否该用户是否有设备号
        if (!isset($reg_ids[0])) {
            return '该用户暂时没有登录设备， 添加失败';
        }
        $faild_msg = _widget('push')->update_device_tags($reg_ids[0], 'add', $info['push_group_id']);
        if ($faild_msg) {
            return '添加失败, 请稍后再试';
        }

        $res = _model('push_group_user')->create($info);
        if ($res) {
            // _widget('log')->record('push_group_user', $res, '新增');
        }

        return array('添加成功!', 'success', 'push/admin/group_user&id='.$info['push_group_id']);
    }

    /**
     * 删除组成员
     * @return array
     */
    public function delete()
    {
        $id = tools_helper::get('id', 0);

        if (!$id) {
            return array('info'=>"要删除的ID不存在!");
        }

        $group_user_info = _uri('push_group_user', $id);
        if (!$group_user_info) {
            return array('info'=>"成员不存在!");
        }

        // $reg_ids = user_helper::get_user_reg_ids($group_user_info['user_id']);
        $reg_ids = _widget('push')->get_user_reg_ids($group_user_info['user_id']);
        $reg_ids = array_values($reg_ids);
        // 检测是否该用户是否有设备号
        if (!isset($reg_ids[0])) {
            return '该用户暂时没有登录设备，删除失败';
        }
        // 通知极光
        $faild_msg = _widget('push')->update_device_tags($reg_ids[0], 'remove', $group_user_info['push_group_id']);
        if ($faild_msg) {
            return '删除失败, 请稍后再试';
        }

        $res = _model('push_group_user')->delete(array('id'=>$id));
        if ($res) {
            // _widget('log')->record('push_group_user', $id, '删除');
        }

        return array('info'=>'ok');
    }
}
?>